package com.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.dao.UserMapper;
import com.pojo.User;
import com.service.UserLoginService;
import com.util.AesStr;
import com.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.crypto.BadPaddingException;

@Service("UserLoginService")
public class UserLoginServiceImpl implements UserLoginService {
    @Autowired
    UserMapper userMapper;
    @Override
    public ResultBean getuserById(User user) {
        ResultBean resultBean = new ResultBean();
        User userInfo=null;
        userInfo = userMapper.getUserbyID(user.getAccount());
        if(userInfo!=null){
            // 解密用户密码进行比对
            String mingWen=null;
            try {
                 mingWen = SaSecureUtil.aesDecrypt(user.getPassword(),userInfo.getPassword());
                if(mingWen.equals(AesStr.getMingWen())){
                    resultBean.setResultObj(user);
                    resultBean.setResultMsg("登录成功！");
                }else {
                    resultBean.setResultMsg("账号或密码不正确！");
                }
            }catch (Exception e){
                resultBean.setResultMsg("账号或密码不正确！");
            }

        }else{
            resultBean.setResultMsg("获取用户信息失败！");
        }
        return resultBean;
    }

    @Override
    public ResultBean UserRegister(User user) {
        ResultBean resultBean = new ResultBean();
        //对用户密码进行加密
        String ciphertext = SaSecureUtil.aesEncrypt(user.getPassword(), AesStr.getMingWen());
        System.out.println("AES加密后：" + ciphertext);
        user.setPassword(ciphertext);
        Integer insert = userMapper.insert(user);
        if(insert>0){
            resultBean.setResultObj(insert);
            resultBean.setResultMsg("注册成功！");
        }else {
            resultBean.setResultMsg("注册失败！");
        }
        return resultBean;
    }

    public static void main(String[] args) {
        String djxh="123";
        if(djxh!=null&&djxh!=""){
            System.out.println("123");
        }
    }
}
